<template>
  <div class="courses-page">
    <main style="margin-top: 0">
      <div class="courses-banner">
        <div class="container">
          <h1>课程中心</h1>
          <p>探索丰富多样的优质课程，开启你的学习之旅</p>
        </div>
      </div>

      <div class="container">
        <div class="filter-section">
          <el-row :gutter="20">
            <el-col :xs="24" :sm="24" :md="16">
              <div class="search-box">
                <el-input
                  v-model="searchQuery"
                  placeholder="搜索课程"
                  prefix-icon="el-icon-search"
                  clearable
                  @input="handleSearch"
                ></el-input>
              </div>
            </el-col>
            <el-col :xs="24" :sm="24" :md="8">
              <div class="filter-box">
                <el-select v-model="categoryFilter" placeholder="课程分类" @change="handleFilter">
                  <el-option label="全部分类" value=""></el-option>
                  <el-option
                    v-for="category in categories"
                    :key="category.id"
                    :label="category.name"
                    :value="category.id"
                  ></el-option>
                </el-select>
                <el-select v-model="sortOption" placeholder="排序方式" @change="handleSort">
                  <el-option label="最新上架" value="newest"></el-option>
                  <el-option label="价格从低到高" value="price-asc"></el-option>
                  <el-option label="价格从高到低" value="price-desc"></el-option>
                  <el-option label="评分最高" value="rating-desc"></el-option>
                </el-select>
              </div>
            </el-col>
          </el-row>
        </div>

        <div class="courses-list">
          <el-row :gutter="20">
            <el-col
              :xs="24"
              :sm="12"
              :md="8"
              :lg="6"
              v-for="course in filteredCourses"
              :key="course.id"
            >
              <el-card shadow="hover" class="course-card">
                <div class="course-image-wrapper">
                  <img
                    :src="course.image"
                    :alt="course.title"
                    class="course-image"
                  >
                  <div class="course-tag" v-if="course.tag">{{ course.tag }}</div>
                </div>
                <div class="course-info">
                  <h3>{{ course.title }}</h3>
                  <p class="course-category">{{ getCategoryName(course.categoryId) }}</p>
                  <div class="price">¥{{ course.price }}
                    <span class="original-price" v-if="course.originalPrice">¥{{ course.originalPrice }}</span>
                  </div>
                  <div class="meta">
                    <span><i class="el-icon-user"></i> {{ course.students }}</span>
                    <span><i class="el-icon-star-off"></i> {{ course.rating }}</span>
                  </div>
                  <AddToCart :product="course" />
                </div>
              </el-card>
            </el-col>
          </el-row>
        </div>

        <div class="empty-state" v-if="filteredCourses.length === 0">
          <el-empty description="没有找到符合条件的课程"></el-empty>
        </div>

        <div class="pagination-container" v-if="filteredCourses.length > 0">
          <el-pagination
            background
            layout="prev, pager, next"
            :total="totalCourses"
            :page-size="pageSize"
            :current-page="currentPage"
            @current-change="handlePageChange"
          ></el-pagination>
        </div>
      </div>
    </main>

    <footer class="footer">
      <div class="container">
        <div class="footer-content">
          <div class="footer-section">
            <h3>关于我们</h3>
            <p>HIIT在线学习平台致力于为学习者提供优质的在线课程资源。</p>
          </div>
          <div class="footer-section">
            <h3>联系方式</h3>
            <p>邮箱：contact@hiit.com</p>
            <p>电话：400-123-4567</p>
          </div>
          <div class="footer-section">
            <h3>关注我们</h3>
            <div class="social-links">
              <i class="el-icon-s-platform"></i>
              <i class="el-icon-s-custom"></i>
              <i class="el-icon-s-promotion"></i>
            </div>
          </div>
        </div>
        <div class="copyright">
          <p>© 2023 HIIT在线学习平台 版权所有</p>
        </div>
      </div>
    </footer>
  </div>
</template>

<script setup>
import { ref, reactive, computed, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import AddToCart from '../components/AddToCart.vue';

const searchQuery = ref('');
const categoryFilter = ref('');
const sortOption = ref('newest');
const currentPage = ref(1);
const pageSize = ref(8);

// 分类数据
const categories = reactive([
  { id: 1, name: '编程开发', desc: '学习最新编程技术', icon: 'el-icon-cpu' },
  { id: 2, name: '设计创意', desc: '提升设计能力', icon: 'el-icon-brush' },
  { id: 3, name: '营销推广', desc: '掌握营销技巧', icon: 'el-icon-data-line' },
  { id: 4, name: '职场技能', desc: '提升职场竞争力', icon: 'el-icon-office-building' },
  { id: 5, name: '语言学习', desc: '掌握多国语言', icon: 'el-icon-chat-line-round' },
  { id: 6, name: '考试认证', desc: '获取专业认证', icon: 'el-icon-document' }
]);

// 课程数据
const allCourses = reactive([
  {
    id: 1,
    categoryId: 1,
    title: 'Vue 3 实战开发',
    image: 'https://picsum.photos/400/300?random=4',
    price: 299,
    originalPrice: 599,
    tag: '限时优惠',
    students: '1254人学习',
    rating: '4.9',
    date: '2023-05-15'
  },
  {
    id: 2,
    categoryId: 1,
    title: 'React 高级课程',
    image: 'https://picsum.photos/400/300?random=5',
    price: 399,
    students: '986人学习',
    rating: '4.8',
    date: '2023-06-20'
  },
  {
    id: 3,
    categoryId: 2,
    title: 'UI设计基础',
    image: 'https://picsum.photos/400/300?random=6',
    price: 199,
    students: '2156人学习',
    rating: '4.7',
    date: '2023-04-10'
  },
  {
    id: 4,
    categoryId: 1,
    title: 'Python数据分析',
    image: 'https://picsum.photos/400/300?random=7',
    price: 349,
    students: '876人学习',
    rating: '4.8',
    date: '2023-07-05'
  },
  {
    id: 5,
    categoryId: 3,
    title: 'Flutter移动开发',
    image: 'https://picsum.photos/400/300?random=8',
    price: 399,
    students: '654人学习',
    rating: '4.7',
    date: '2023-03-25'
  },
  {
    id: 6,
    categoryId: 4,
    title: '职场沟通技巧',
    image: 'https://picsum.photos/400/300?random=9',
    price: 149,
    originalPrice: 299,
    tag: '热门',
    students: '3254人学习',
    rating: '4.9',
    date: '2023-02-18'
  },
  {
    id: 7,
    categoryId: 5,
    title: '商务英语入门',
    image: 'https://picsum.photos/400/300?random=10',
    price: 199,
    students: '1876人学习',
    rating: '4.6',
    date: '2023-01-30'
  },
  {
    id: 8,
    categoryId: 6,
    title: 'PMP项目管理认证',
    image: 'https://picsum.photos/400/300?random=11',
    price: 599,
    originalPrice: 899,
    tag: '认证课程',
    students: '754人学习',
    rating: '4.8',
    date: '2023-04-22'
  },
  {
    id: 9,
    categoryId: 2,
    title: 'Photoshop高级技巧',
    image: 'https://picsum.photos/400/300?random=12',
    price: 249,
    students: '1432人学习',
    rating: '4.7',
    date: '2023-05-08'
  },
  {
    id: 10,
    categoryId: 3,
    title: '数字营销策略',
    image: 'https://picsum.photos/400/300?random=13',
    price: 329,
    students: '987人学习',
    rating: '4.5',
    date: '2023-06-12'
  },
  {
    id: 11,
    categoryId: 4,
    title: '领导力培训',
    image: 'https://picsum.photos/400/300?random=14',
    price: 499,
    students: '645人学习',
    rating: '4.9',
    date: '2023-07-20'
  },
  {
    id: 12,
    categoryId: 5,
    title: '日语N2备考',
    image: 'https://picsum.photos/400/300?random=15',
    price: 279,
    originalPrice: 399,
    tag: '限时优惠',
    students: '876人学习',
    rating: '4.8',
    date: '2023-03-15'
  }
]);

// 过滤和排序课程
const filteredAndSortedCourses = computed(() => {
  let result = [...allCourses];
  
  // 应用分类过滤
  if (categoryFilter.value) {
    result = result.filter(course => course.categoryId === categoryFilter.value);
  }
  
  // 应用搜索过滤
  if (searchQuery.value) {
    const query = searchQuery.value.toLowerCase();
    result = result.filter(course => 
      course.title.toLowerCase().includes(query) || 
      getCategoryName(course.categoryId).toLowerCase().includes(query)
    );
  }
  
  // 应用排序
  switch (sortOption.value) {
    case 'newest':
      result.sort((a, b) => new Date(b.date) - new Date(a.date));
      break;
    case 'price-asc':
      result.sort((a, b) => a.price - b.price);
      break;
    case 'price-desc':
      result.sort((a, b) => b.price - a.price);
      break;
    case 'rating-desc':
      result.sort((a, b) => parseFloat(b.rating) - parseFloat(a.rating));
      break;
  }
  
  return result;
});

// 分页后的课程
const filteredCourses = computed(() => {
  const start = (currentPage.value - 1) * pageSize.value;
  const end = start + pageSize.value;
  return filteredAndSortedCourses.value.slice(start, end);
});

// 总课程数
const totalCourses = computed(() => {
  return filteredAndSortedCourses.value.length;
});

// 方法
const handleSelect = (key) => {
  if (key === '1') {
    goToHome();
  }
};

const goToHome = () => {
  router.push({ name: 'Home' });
};

const handleSearch = () => {
  currentPage.value = 1;
};

const handleFilter = () => {
  currentPage.value = 1;
};

const handleSort = () => {
  currentPage.value = 1;
};

const handlePageChange = (page) => {
  currentPage.value = page;
  window.scrollTo({ top: 0, behavior: 'smooth' });
};

const getCategoryName = (categoryId) => {
  const category = categories.find(c => c.id === categoryId);
  return category ? category.name : '';
};

onMounted(() => {
  // 页面加载时滚动到顶部
  window.scrollTo(0, 0);
});
</script>

<style scoped>
.courses-page {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.container {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 15px;
}

.header {
  background-color: #fff;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 1000;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  height: 60px;
}

.logo {
  font-size: 1.5rem;
  font-weight: bold;
  color: #409EFF;
  cursor: pointer;
}

.user-actions {
  display: flex;
  gap: 10px;
}

.courses-banner {
  background: linear-gradient(135deg, #409EFF 0%, #1890ff 100%);
  color: white;
  padding: 60px 0;
  text-align: center;
  margin-bottom: 40px;
}

.courses-banner h1 {
  font-size: 2.5rem;
  margin-bottom: 15px;
}

.courses-banner p {
  font-size: 1.2rem;
  opacity: 0.9;
}

.filter-section {
  margin-bottom: 30px;
}

.search-box {
  margin-bottom: 20px;
}

.filter-box {
  display: flex;
  gap: 15px;
}

.filter-box .el-select {
  width: 50%;
}

.courses-list {
  margin-bottom: 50px;
}

.course-card {
  height: 100%;
  margin-bottom: 25px;
  overflow: hidden;
  transition: all 0.3s;
}

.course-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
}

.course-image-wrapper {
  position: relative;
  overflow: hidden;
}

.course-image {
  width: 100%;
  height: 180px;
  object-fit: cover;
  transition: transform 0.3s;
}

.course-card:hover .course-image {
  transform: scale(1.05);
}

.course-tag {
  position: absolute;
  top: 10px;
  right: 10px;
  background-color: #F56C6C;
  color: white;
  padding: 5px 10px;
  border-radius: 4px;
  font-size: 12px;
}

.course-info {
  padding: 15px;
}

.course-info h3 {
  margin-bottom: 10px;
  color: #303133;
  font-size: 16px;
  height: 44px;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.course-category {
  color: #909399;
  font-size: 14px;
  margin-bottom: 10px;
}

.price {
  color: #F56C6C;
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 10px;
}

.original-price {
  color: #909399;
  font-size: 14px;
  text-decoration: line-through;
  margin-left: 5px;
}

.meta {
  display: flex;
  justify-content: space-between;
  color: #909399;
  font-size: 12px;
  margin-bottom: 15px;
}

.enroll-btn {
  width: 100%;
}

.pagination-container {
  display: flex;
  justify-content: center;
  margin: 40px 0;
}

.empty-state {
  padding: 40px 0;
  text-align: center;
}

.footer {
  background-color: #303133;
  color: #E4E7ED;
  padding: 40px 0;
  margin-top: auto;
}

.footer-content {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  margin-bottom: 20px;
}

.footer-section {
  min-width: 30%;
  margin-bottom: 20px;
}

.footer-section h3 {
  margin-bottom: 15px;
  color: #fff;
  font-size: 18px;
}

.social-links {
  display: flex;
  gap: 15px;
  font-size: 24px;
}

.social-links i {
  cursor: pointer;
  transition: color 0.3s;
}

.social-links i:hover {
  color: #409EFF;
}

.copyright {
  text-align: center;
  padding-top: 20px;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .container {
    padding: 0 30px;
  }
}

@media (max-width: 992px) {
  .courses-banner h1 {
    font-size: 2rem;
  }
  
  .courses-banner p {
    font-size: 1rem;
  }
}

@media (max-width: 768px) {
  .header {
    padding: 0 15px;
  }
  
  .logo {
    font-size: 1.2rem;
  }
  
  .filter-box {
    flex-direction: column;
  }
  
  .filter-box .el-select {
    width: 100%;
  }
  
  .courses-banner {
    padding: 40px 0;
  }
  
  .courses-banner h1 {
    font-size: 1.5rem;
  }
  
  .footer-section {
    min-width: 100%;
    text-align: center;
  }
}

@media (max-width: 576px) {
  .course-image {
    height: 150px;
  }
  
  .course-info h3 {
    font-size: 14px;
    height: 38px;
  }
  
  .price {
    font-size: 16px;
  }
  
  .meta {
    font-size: 11px;
  }
  
  .enroll-btn {
    font-size: 12px;
    padding: 8px 12px;
  }
}
</style>